package com.control.controller;


import com.control.entity.Contact;
import com.control.entity.vo.UserInfoVO;
import com.control.service.ContactService;
import com.control.util.PageData;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class MyController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        ContactService serivce = context.getBean(ContactService.class);
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String op = req.getParameter("op");
        if (op.equals("index")) {
            int pageIndex = 1;
            String strPage = req.getParameter("page");
            if (strPage != null) {
                pageIndex = Integer.parseInt(strPage);
            }
            int pageSize = 4;

         PageData<Contact> userInfoList=serivce.getPage(pageIndex,pageSize);

            UserInfoVO vo = new UserInfoVO(userInfoList);
            //结果存入作用域
            req.setAttribute("vo", vo);


            req.getRequestDispatcher("index.jsp").forward(req, resp);
        }
    }
}
